package com.qiyang.contoller;

import com.qiyang.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created on 2020/12/9.
 *
 * @author qiyang.
 * 描述：RequestMapping注解学习
 */
@RequestMapping("/mapping")
public class MappingController {

    /**
     * params: 必须有username 必须不能有passwd 必须有sex=1
     *
     * @return
     */
    @PostMapping(value = "/method1", params = {"username", "!passwd", "sex=1"})
    public String method1() {

        return "/index.jsp";
    }

    /**
     * headers: 请求头必须有xxx=xxx才能访问
     *
     * @return
     */
    @PostMapping(value = "/header1", headers = {"Accpt-Language=zh-CN,zh;q=0.9"})
    public String header1() {

        return "/index.jsp";
    }

    /**
     * consumes: 请求内容类型必须为xxx
     * 场景请求内容类型：application/x-www-form-urlencoded | multipart/form-data | application/json
     *
     * @return
     */
    @PostMapping(value = "/consumes1", consumes = {"application/x-www-form-urlencoded"})
    public String consumes1() {

        return "/index.jsp";
    }

    /**
     * consumes: 响应的内容类型必须为xxx
     *
     * @return
     */
    @PostMapping(value = "/produces1", produces = {"application/json"})
    public String produces1() {

        return "/index.jsp";
    }

    /**
     * 通配符：
     * ? 任意单个字符 /a
     * * 任意多个字符 /ant
     * ** 任意多级匹配 /ant/123
     * 映射存在多个可匹配时，以最精确为优先，没有通配符优先级最高
     *
     * @return
     */
    @PostMapping(value = "/ant?*", produces = {"application/json"})
    public String ant() {

        return "/index.jsp";
    }

    /**
     * Restfull 风格，支持连续写多个
     *
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public String rest(@PathVariable("id") Integer id) {

        return "/index.jsp";
    }

    /**
     * 如果是对象，无需加@PathVariable注解
     * 注意参数名要与属性名一致
     * @param user
     * @return
     */
    @GetMapping("/user/{id}/{username}")
    public String rest(User user) {

        return "/index.jsp";
    }

}
